package model;

import java.awt.Graphics;

/**
 * <p>
 * Title: DrawableEdge class</p>
 * <p>
 * Description: CS 343 Project. Allows edges to be to drawn on the graphics.</p>
 *
 * @date 11/18/2013
 *
 * @author Dylan Ahmann
 * @author Gregg Roemhildt - Class Programming and JavaDocs
 * @author Sam Brockmann - JavaDocs
 * @author Emily Hancock
 * @author Shirin Asadi
 *
 *
 */
public class DrawableEdge extends Edge<double[]> implements Drawable {

    /**
     * Initializes the a and b, which are the "To" and "From" values of this
     * edge.
     *
     * @param a First Vertex
     * @param b Second Vertex
     *
     */
    public DrawableEdge(Vertex<double[]> a, Vertex<double[]> b) {
        super(a, b);
        double distance = calcDistance(a.pointData, b.pointData);
        super.setWeight(distance);
    }

    /**
     * Initializes the a and b (which are the "To" and "From" values), non
     * default weight of this edge, and the weight.
     *
     * @param a First Vertex
     * @param b Second Vertex
     * @param w weight
     *
     */
    public DrawableEdge(Vertex<double[]> a, Vertex<double[]> b, double w) {
        super(a, b, w);
        double distance = calcDistance(a.pointData, b.pointData);
        super.setWeight(distance);
    }

    /**
     * Performs deep copy.
     *
     * @param e the Edge
     *
     */
    public DrawableEdge(Edge<double[]> e) {
        super(e);
    }

    /**
     * Paints an edge on the map.
     *
     * @param g Graphics instance
     *
     */
    @Override
    public void paint(Graphics g) {
        g.drawLine((int) a.pointData[0], (int) a.pointData[1],
                (int) b.pointData[0], (int) b.pointData[1]);
    }

    /**
     *
     * @param g graphics to paint on
     * @param x x offset
     * @param y y offset
     * @param z zoom level
     */
    public void paint(Graphics g, double x, double y, double z) {
        int x1 = (int) ((a.pointData[0] + x) * z);
        int y1 = (int) ((a.pointData[1] + y) * z);
        int x2 = (int) ((b.pointData[0] + x) * z);
        int y2 = (int) ((b.pointData[1] + y) * z);

        g.drawLine(x1, y1, x2, y2);
    }

    /**
     * Calculate the distance based on the Pythagorean theorem. Takes two pairs
     * of integers in array1 and array2.
     * <p>
     * distance = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )</p>
     *
     * @param a First set of x,y coords
     * @param b Second set of x,y coords
     * @return the distance between the two points
     *
     *
     */
    protected final double calcDistance(double[] a, double[] b) {
        return Math.sqrt(
                Math.pow((b[0] - a[0]), 2)
                + Math.pow((b[1] - a[1]), 2));
    }
}
